package com.qiujie.controller;

import com.qiujie.dto.ResponseDTO;
import com.qiujie.entity.DiningOrder;
import com.qiujie.service.DiningOrderService;
import com.qiujie.service.StaffService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;

/**
 * 订单控制器
 */
@RestController
@RequestMapping("/order")
@Api(tags = "订单管理")
public class DiningOrderController {

    @Autowired
    private DiningOrderService diningOrderService;

    @Autowired
    private StaffService staffService;

    @GetMapping("/statistics")
    @ApiOperation("订单数量趋势统计（最近7天）")
    public ResponseDTO statistics() {
        ResponseDTO data = diningOrderService.statistics();
        return data;
    }

    @PostMapping("/create")
    @ApiOperation("在线点餐")
    @PreAuthorize("hasAnyAuthority('order:create')")
    public ResponseDTO create(@RequestBody DiningOrder diningOrder) {
        Integer staffId = diningOrder.getStaffId();
        return this.diningOrderService.create(diningOrder);
    }

    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单：状态: 0已取消 1待确认 2已确认")
    public ResponseDTO cancel(@PathVariable Integer id) {
        // 获取当前认证用户
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();

        // 或者获取自定义用户对象
         UserDetails userDetails = (UserDetails) authentication.getPrincipal();

        return this.diningOrderService.cancel(id);
    }

    @PutMapping("/confirm/{id}")
    @ApiOperation("确认订单：状态: 0已取消 1待确认 2已确认")
    public ResponseDTO confirm(@PathVariable Integer id) {

        // 获取当前认证用户
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();

        // 或者获取自定义用户对象
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        return this.diningOrderService.confirm(id);
    }

    @GetMapping("/list")
    @ApiOperation("获取订单列表")
    public ResponseDTO list(@RequestParam(required = false) Integer elderlyStaffId,
                            @RequestParam(required = false) Integer staffId,
                            @RequestParam(required = false) Integer status,
                            @RequestParam(required = false) Date startDate,
                            @RequestParam(required = false) Date endDate) {
        return this.diningOrderService.list(elderlyStaffId, staffId, status, startDate, endDate);
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("获取订单详情")
    public ResponseDTO getDetail(@PathVariable Integer id) {
        return this.diningOrderService.getDetail(id);
    }

//    @GetMapping("/statistics")
//    @ApiOperation("获取订单统计")
//    public ResponseDTO getStatistics(@RequestParam(required = false) Date startDate,
//                                     @RequestParam(required = false) Date endDate) {
//        return this.diningOrderService.getStatistics(startDate, endDate);
//    }
} 